import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:village_android/config/contants.dart';
import 'package:village_android/pages/home_page.dart';
import 'package:village_android/pages/party_manager_page.dart';
import 'package:village_android/pages/profile_page.dart';
import 'package:village_android/pages/village/village_manager_page.dart';
import 'package:village_android/utils/store_utils.dart';



class IndexPage extends StatefulWidget {

  var userId;
  IndexPage(this.userId);

  List<BottomNavigationBarItem> tabItems = [
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.home),
      title: Text("首页"),
    ),
    BottomNavigationBarItem(
      icon: new Image.network(kImageHost + 'party_tab.png',height: 20,fit: BoxFit.contain,),
      activeIcon: new Image.network(kImageHost + 'tab_dangwu_select.png',height: 20,fit: BoxFit.contain,),
      title: Text("党务管理"),
    ),
    BottomNavigationBarItem(
      icon: new Image.network(kImageHost + 'tab_cunwu.png',height: 20 ,fit: BoxFit.contain),
      activeIcon: new Image.network(kImageHost + 'tab_cunwu_select.png',height: 20 ,fit: BoxFit.contain),
      title: Text("村务管理"),
    ),
    BottomNavigationBarItem(
      icon: Icon(CupertinoIcons.profile_circled),
      title: Text("我的"),
    ),
  ];

  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {


  int currentIndex = 0;
  var currentPage;
  final List<Widget> pages = [
    HomePage(),
    PartyManagerPage(),
    VillageManagerPage(),
    ProfilePage()
  ];


  @override
  void initState() {
    currentPage = pages[currentIndex];
    super.initState();

  }

  SharedPreferences sharedPrefs;

  Future<void> _getPrefs() async{
    sharedPrefs = await SharedPreferences.getInstance();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance =
    ScreenUtil(width: IPHONE6_WIDTH, height: IPHONE6_HEIGHT)..init(context);

    return FutureBuilder(
      future: _getPrefs(),
      builder: (context, snapshot) {
        return Scaffold(
          backgroundColor: Colors.white70,
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            items: widget.tabItems,
            currentIndex: currentIndex,
            onTap: (index) {
              setState(()  {

                if(index==3 && sharedPrefs.get(SharedPreferenceUtil.USER_ID)==null){
                  Navigator.of(context).pushNamedAndRemoveUntil(
                      "/login", ModalRoute.withName("/login"));
                }

                currentIndex = index;
                currentPage = pages[currentIndex];
              });
            },
          ),
          body: IndexedStack(
            index: currentIndex,
            children: pages,
          ),
        ); // or some other widget
      },
    );


  }
}

//
